<template>
  <el-cascader
    :value="value"
    :options="options"
    separator=" - "
    :props="paramsProps"
    @change="handleChange"
  />
</template>

<script>
import { getDepartments } from '@/api/department'
import { transListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // value: [],
      paramsProps: {
        label: 'name', // label控制要显示的内容
        value: 'id' // value控制存储数据的内容
      },
      options: []
    }
  },
  created() {
    this.initDepartment()
  },
  methods: {
    handleChange(data) {
      console.log(data, 'data')
      if (data.length > 0) {
        //  data[data.length - 1] 最后一个部门的id
        this.$emit('input', data[data.length - 1])
      }
    },
    async initDepartment() {
      const depts = await getDepartments()
      this.options = transListToTreeData(depts, 0)
    }
  }
}

</script>

<style>

</style>
